/*
 * Copyright (C) 2013 Andreas Stuetz <andreas.stuetz@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.changil.nowonchangilchurch.page;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.changil.nowonchangilchurch.R;
import com.changil.nowonchangilchurch.protocol.ArticleManager;
import com.changil.nowonchangilchurch.protocol.dto.ArticleList;
import com.changil.nowonchangilchurch.protocol.http.DataLoadListener;
import com.changil.nowonchangilchurch.widget.asyncimagegetter.HtmlSetter;

public class LatestDetailFragment extends BaseNetworkFragment {
	private int mBoardId = 0;
	private TextView mText = null;
	
	public LatestDetailFragment() {
		
	}
	
	private LatestDetailFragment(int boardId){
		mBoardId = boardId;
	}
	
	public static LatestDetailFragment newInstance(int boardId) {
		LatestDetailFragment f = new LatestDetailFragment(boardId);
		return f;
	}

	@Override
	protected View createContentView(LayoutInflater inflater) {
		View view = inflater.inflate(R.layout.fragment_latest_detail, null);
		mText = (TextView)view.findViewById(R.id.fragment_latest_detail);
		return view;
	}
	
	@Override
	protected void loadData() {
		ArticleManager.getsInstance().getArticleList(mDataLoadListener,mBoardId,Integer.MAX_VALUE, 1, true);
	}
	
	private DataLoadListener<ArticleList> mDataLoadListener = new DataLoadListener<ArticleList>() {
		
		@Override
		public void onSuccess(ArticleList response) {
			setLoadingView(false);
			if(null != response.list && 0 < response.list.size()) {
				HtmlSetter.newInstanceWithImageLoad(getActivity(), mText, response.list.get(0).content).setTextViewHTML(true);
			}
		}
		
		@Override
		public void onFail() {
			setLoadingView(false);
			setRetryView(true);
		}
	};
	
	public String getContent() {
		return mText.getText().toString();
	}
}